# SPDX-License-Identifier: GPL-2.0
# ==========================================================================
# Installing headers
#
# All headers under include/uapi, include/generated/uapi,
# arch/<arch>/include/uapi and arch/<arch>/include/generated/uapi are
# exported.
# They are preprocessed to remove __KERNEL__ section of the file.
#
# ==========================================================================

PHONY := __headers
__headers:

include scripts/Kbuild.include

srcdir        := $(srctree)/$(obj)

# When make is run under a fakechroot environment, the function
# $(wildcard $(srcdir)/*/.) doesn't only return directories, but also regular
# files. So, we are using a combination of sort/dir/wildcard which works
# with fakechroot.
subdirs       := $(patsubst $(srcdir)/%/,%,\
		 $(filter-out $(srcdir)/,\
		 $(sort $(dir $(wildcard $(srcdir)/*/)))))

# Recursion
__headers: $(subdirs)

PHONY += $(subdirs)
$(subdirs):
	$(Q)$(MAKE) $(hdr-inst)=$(obj)/$@ dst=$(dst)/$@

# Skip header install/check for include/uapi and arch/$(SRCARCH)/include/uapi.
# We have only sub-directories there.
skip-inst := $(if $(filter %/uapi,$(obj)),1)

ifeq ($(skip-inst),)

# Kbuild file is optional
kbuild-file := $(srctree)/$(obj)/Kbuild
-include $(kbuild-file)

installdir    := $(INSTALL_HDR_PATH)/$(dst)
gendir        := $(objtree)/$(subst include/,include/generated/,$(obj))
header-files  := $(notdir $(wildcard $(srcdir)/*.h))
header-files  += $(notdir $(wildcard $(srcdir)/*.agh))
header-files  := $(filter-out $(no-export-headers), $(header-files))
genhdr-files  := $(notdir $(wildcard $(gendir)/*.h))
genhdr-files  := $(filter-out $(header-files), $(genhdr-files))

# files used to track state of install/check
install-file  := $(installdir)/.install
check-file    := $(installdir)/.check

# all headers files for this dir
all-files     := $(header-files) $(genhdr-files)
output-files  := $(addprefix $(installdir)/, $(all-files))

ifneq ($(mandatory-y),)
missing       := $(filter-out $(all-files),$(mandatory-y))
ifneq ($(missing),)
$(error Some mandatory headers ($(missing)) are missing in $(obj))
endif
endif

# Work out what needs to be removed
oldheaders    := $(patsubst $(installdir)/%,%,$(wildcard $(installdir)/*.h))
unwanted      := $(filter-out $(all-files),$(oldheaders))

# Prefix unwanted with full paths to $(INSTALL_HDR_PATH)
unwanted-file := $(addprefix $(installdir)/, $(unwanted))

printdir = $(patsubst $(INSTALL_HDR_PATH)/%/,%,$(dir $@))

quiet_cmd_install = INSTALL $(printdir) ($(words $(all-files))\
                            file$(if $(word 2, $(all-files)),s))
      cmd_install = \
        $(CONFIG_SHELL) $< $(installdir) $(srcdir) $(header-files); \
        $(CONFIG_SHELL) $< $(installdir) $(gendir) $(genhdr-files); \
        touch $@

quiet_cmd_remove = REMOVE  $(unwanted)
      cmd_remove = rm -f $(unwanted-file)

quiet_cmd_check = CHECK   $(printdir) ($(words $(all-files)) files)
# Headers list can be pretty long, xargs helps to avoid
# the "Argument list too long" error.
      cmd_check = for f in $(all-files); do                          \
                  echo "$(installdir)/$${f}"; done                      \
                  | xargs                                            \
                  $(PERL) $< $(INSTALL_HDR_PATH)/include $(SRCARCH); \
	          touch $@

ifndef HDRCHECK
# Rules for installing headers
__headers: $(install-file)
	@:

targets += $(install-file)
$(install-file): scripts/headers_install.sh \
		 $(addprefix $(srcdir)/,$(header-files)) \
		 $(addprefix $(gendir)/,$(genhdr-files)) FORCE
	$(if $(unwanted),$(call cmd,remove),)
	$(if $(wildcard $(dir $@)),,$(shell mkdir -p $(dir $@)))
	$(call if_changed,install)

else
__headers: $(check-file)
	@:

targets += $(check-file)
$(check-file): scripts/headers_check.pl $(output-files) FORCE
	$(call if_changed,check)

endif

cmd_files := $(wildcard \
             $(foreach f,$(sort $(targets)),$(dir $(f)).$(notdir $(f)).cmd))

ifneq ($(cmd_files),)
	include $(cmd_files)
endif

endif # skip-inst

PHONY += FORCE
FORCE: ;

.PHONY: $(PHONY)
